package com.ruoyi.rsikManage.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileUploadController {


    @PostMapping("/upload/1")
    public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) {
        if (file.isEmpty()) {
            return "请选择要上传的文件";
        }

        try {
            // 获取文件的原始名称
            UUID uuid = UUID.randomUUID();
            String fileName = uuid.toString();
            String storagePath = "";

            if (type.equals("image")) {
                storagePath = "D:/zdzh/Image/";
            } else {
                storagePath = "D:/zdzh/Video/";

            }

            // 指定存储目录

            // 创建目标文件对象
            File destFile = new File(storagePath + fileName);

            // 检查目标目录是否存在，不存在则创建
            File destDirectory = destFile.getParentFile();
            if (!destDirectory.exists()) {
                destDirectory.mkdirs();
            }

            // 将文件保存到目标位置
            FileOutputStream fos = new FileOutputStream(destFile);
            fos.write(file.getBytes());
            fos.close();

            return "文件上传成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "文件上传失败";
        }
    }
//
}
